let baseUrlStr = 'https://coder.itclan.cn?name=itclanCoder&study=css';
// 参数转成对象
function queryString(str) {
  let params = str.split('?')[1]; //截取?号后的字符串即name=itclanCoder&study=css
  let param = params.split('&'); // 通过&符号进行分割即["name=itclanCoder", "study=css"]
  let obj = {}; // 用一个对象存储目标值
  for (let i = 0; i < param.length; i++) {
    // 循环遍历截取出来的param数组
    let paramsA = param[i].split('='); // 通过split,=继续对数组params每一项进行分割,生成数组["name", "itclanCoder"]
    let key = paramsA[0]; // 取数组项["name", "itclanCoder"]中第0位,即name
    let value = paramsA[1]; // 取数组项["name", "itclanCoder"]中第1位,即itclanCoder
    obj[key] = value;
  }
  return obj;
}
console.log(queryString(url)); // {name: "itclanCoder", study: "css"]}


class URLSearchParams {
  #searchParams = []

  constructor(init) {
    if (typeof init === "string") {
      this.#searchParams = init.split("&").map(kv => kv.split("="))
    } else {
      this.#searchParams = Object.entries(init)
    }
  }

  get(key) {
    const param = this.#searchParams.find(param => param[0] === key)
    return param && param[1]
  }

  set(key, value) {
    const param = this.#searchParams.find(param => param[0] === key)

    if (param) {
      param[1] = value
    } else {
      this.#searchParams.push([key, value])
    }
  }

  has(key) {
    return this.#searchParams.some(param => param[0] === key)
  }

  append(key, value) {
    this.#searchParams.push([key, value])
  }

  toString() {
    return this.#searchParams.map(param => param.join("=")).join("&")
  }

  *[Symbol.iterator]() {
    yield* this.#searchParams
  }
} 